//
//  MyIntegralViewController.m
//  Findif
//
//  Created by Mac on 2019/4/11.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "MyIntegralViewController.h"
#import "MyIntegralTableViewCell.h"
#import "ExchangeViewController.h"
#import "MyIntegralModel.h"
#import "PGDatePickManager.h"
#import "ServerStatementViewController.h"

@interface MyIntegralViewController ()<UITableViewDataSource,UITableViewDelegate,PGDatePickerDelegate>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, weak)UIView * topView;
@property (nonatomic, weak)UIImageView * iconImageView;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, weak)UIImageView * genderImageView;
@property (nonatomic, weak)UILabel * ageLabel;
@property (nonatomic, weak)UILabel * numberLabel;
@property (nonatomic, weak)UILabel * useLabel;
@property (nonatomic, weak)UIButton * exchangeButton;
@property (nonatomic, weak)UIImageView * rightImageView;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UILabel * monthLabel;
@property (nonatomic, weak)UIButton * calenderButton;
@property (nonatomic, weak)UIImageView * spaceImageView;
@property (nonatomic, weak)UILabel * spaceLabel;
@property (nonatomic, strong)NSArray<MyIntegralModel *>* modelArr;


@end

@implementation MyIntegralViewController

- (void)viewWillAppear:(BOOL)animated{
    
   [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ColorWithRGB(0xEFF0F1);
    
    
    [self setupNav];
    [self setupUI];
}

- (void)loadData{
    
    NSString *ID = DEF_GET_OBJECT(@"id");
    
    [[ZRNetworkTool sharedTool] getUserInfoResultWithUser_id:ID WithExamine_user_id:@"" WithBlock:^(BOOL success, id responseData) {
        if (success) {
            
            NSDictionary * dict = responseData[@"data"];
            if ([dict[@"head_img"] isEqual:[NSNull null]]) {
                
                self.iconImageView.image = [UIImage imageNamed:@"组12"];
            }else{
                
                if([dict[@"head_img"] containsString:@"http"]) {
                    
                    NSURL * url = [NSURL URLWithString:dict[@"head_img"]];
                    [self.iconImageView sd_setImageWithURL:url];
                } else {
                    
                    NSString * iconStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dict[@"head_img"]];
                    NSURL * url = [NSURL URLWithString:iconStr];
                    [self.iconImageView sd_setImageWithURL:url];
                }
            }
            
            
            if ([dict[@"user_name"] isEqual:[NSNull null]]) {
                
                self.nameLabel.text = @"----";
            }else{
                
                self.nameLabel.text = dict[@"user_name"];
            }
            
            if ([dict[@"age"] isEqual:[NSNull null]]) {
                
                self.ageLabel.text = @"";
            }else{
                
                NSString * age = [NSString stringWithFormat:@"%@",dict[@"age"]];
                self.ageLabel.text = [NSString stringWithFormat:@"%@",age];
            }
            
            self.numberLabel.text = [NSString stringWithFormat:@"%@",dict[@"user_integral_number"]];
            
            int sex = [dict[@"sex"] intValue];
            if (sex == 1) {
                
                self.genderImageView.image = [UIImage imageNamed:@"男性"];
            }else{
                
                self.genderImageView.image = [UIImage imageNamed:@"女性"];
            }
        }
    }];
    
    [[ZRNetworkTool sharedTool] getUserIntegralWithDate_time:@"" WithBlock:^(BOOL success, id responseData) {

        if (success) {
            
            self.modelArr = [NSArray yy_modelArrayWithClass:[MyIntegralModel class] json:responseData[@"data"]];
        }
    }];
}

- (void)setModelArr:(NSArray<MyIntegralModel *> *)modelArr{

    _modelArr = modelArr;
    
    [self.tableView reloadData];
}

- (void)setupNav{
    
    self.title = @"我的积分";
    
    UIButton * addButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25 * SCREENH6, 25 * SCREENH6)];
    
    [addButton setImage:[UIImage imageNamed:@"问号"] forState:UIControlStateNormal];
    
    [addButton addTarget:self action:@selector(clickQuestionButton) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] initWithCustomView:addButton];
    
    self.navigationItem.rightBarButtonItem = rightButton;
}


- (void)setupUI{
    
    UIView * topView = [[UIView alloc] initWithFrame:CGRectMake(15 * SCREENH6, topHeight + 10 * SCREENH6, 345 * SCREENH6, 90 * SCREENH6)];
    
    topView.backgroundColor = ZRWhiteColor;
    topView.userInteractionEnabled = YES;
    topView.layer.cornerRadius = 5 * SCREENH6;
    topView.layer.masksToBounds = YES;
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    
    UIImageView * iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15 * SCREENH6, 20 * SCREENH6, 50 * SCREENH6, 50 * SCREENH6)];
    
//    iconImageView.backgroundColor = [UIColor redColor];
    iconImageView.layer.cornerRadius = 25 * SCREENH6;
    iconImageView.layer.masksToBounds = YES;
    
    self.iconImageView = iconImageView;
    
    [self.topView addSubview:self.iconImageView];
    
    UILabel * nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(75 * SCREENH6, 20 * SCREENH6, 150 * SCREENH6, 15 * SCREENH6)];
    
    nameLabel.textColor = ZRBlackColor;
    nameLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    nameLabel.text = @"哗哈哈";
//    [nameLabel sizeToFit];
    
    self.nameLabel = nameLabel;
    
    [self.topView addSubview:self.nameLabel];
    
    
    UIImageView * genderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(75 * SCREENH6, 48 * SCREENH6, 13 * SCREENH6, 13 * SCREENH6)];
    
    genderImageView.image = [UIImage imageNamed:@"女性"];
    
    self.genderImageView = genderImageView;
    
    [self.topView addSubview:self.genderImageView];
    
    
    UILabel * ageLabel = [UILabel new];
    
    ageLabel.text = @"90后";
    ageLabel.textColor = ZRBlackColor;
    ageLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    self.ageLabel = ageLabel;
    
    [self.topView addSubview:self.ageLabel];
    
    
    UILabel * numberLabel = [UILabel new];
    
    numberLabel.text = @"2020";
    numberLabel.textColor = ZRBlackColor;
    numberLabel.textAlignment = NSTextAlignmentCenter;
    numberLabel.font = [UIFont boldSystemFontOfSize:20 * SCREENH6];
    
    self.numberLabel = numberLabel;
    
    [self.topView addSubview:self.numberLabel];
    
    
    UILabel * useLabel = [UILabel new];
    
    useLabel.textColor = [UIColor lightGrayColor];
    useLabel.font = [UIFont systemFontOfSize:11 * SCREENH6];
    useLabel.text = @"可用积分";
    
    self.useLabel = useLabel;
    
    [self.topView addSubview:self.useLabel];
    
    
    UIButton * exchangeButton = [UIButton new];
    
    [exchangeButton setTitle:@"兑换" forState:UIControlStateNormal];
    [exchangeButton setTitleColor:ColorWithRGB(0xFF5C5C) forState:UIControlStateNormal];
    [exchangeButton addTarget:self action:@selector(clickExchangeButton) forControlEvents:UIControlEventTouchUpInside];
    exchangeButton.titleLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    self.exchangeButton = exchangeButton;
    
    [self.topView addSubview:self.exchangeButton];
    
    
    UIImageView * rightImageView = [UIImageView new];
    
    rightImageView.image = [UIImage imageNamed:@"兑换-展开"];
    
    self.rightImageView = rightImageView;
    
    [self.topView addSubview:self.rightImageView];
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.text = @"积分记录";
    titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    titleLabel.textColor = ZRBlackColor;
    
    self.titleLabel = titleLabel;
    
    [self.view addSubview:self.titleLabel];
    
    
    UILabel * monthLabel = [UILabel new];
    
    monthLabel.text = @"(本月)";
    monthLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    monthLabel.textColor = [UIColor lightGrayColor];
    
    self.monthLabel = monthLabel;
    
    [self.view addSubview:self.monthLabel];
    
    
    UIButton * calenderButton = [UIButton new];
    
    [calenderButton setImage:[UIImage imageNamed:@"日历"] forState:UIControlStateNormal];
    [calenderButton addTarget:self action:@selector(clickCalenderButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.calenderButton = calenderButton;
    
    [self.view addSubview:self.calenderButton];
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topHeight + 147 * SCREENH6, kScreenW,kScreenH - topHeight - 147 * SCREENH6) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xEFF0F1);
    tableView.rowHeight = 50 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[MyIntegralTableViewCell class] forCellReuseIdentifier:@"MyIntegralTableViewCell"];
    
    //    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    //
    //        [self updateData];
    //    }];
    
    [self.view addSubview:self.tableView];
    
    
    UIImageView * spaceImageView = [UIImageView new];
    
    spaceImageView.image = [UIImage imageNamed:@"状态为空"];
    spaceImageView.hidden = YES;
    
    self.spaceImageView = spaceImageView;
    
    [self.tableView addSubview:self.spaceImageView];
    
    
    UILabel * spaceLabel = [UILabel new];
    
    spaceLabel.text = @"暂无积分记录";
    spaceLabel.textColor = [UIColor lightGrayColor];
    spaceLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    spaceLabel.textAlignment = NSTextAlignmentCenter;
    spaceLabel.hidden = YES;
    
    self.spaceLabel = spaceLabel;
    
    [self.tableView addSubview:self.spaceLabel];
}


- (void)viewDidLayoutSubviews{
    
    [self.ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.genderImageView);
        make.leading.mas_equalTo(self.genderImageView.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.useLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.topView).offset(-65 * SCREENH6);
        make.centerY.mas_equalTo(self.topView);
    }];
    
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.useLabel);
        make.bottom.mas_equalTo(self.useLabel.mas_top).offset(-5 * SCREENH6);
    }];
    
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.topView).offset(-65 * SCREENH6);
        make.top.mas_equalTo(self.useLabel.mas_bottom).offset(5 * SCREENH6);
        make.width.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.exchangeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.rightImageView.mas_leading);
        make.centerY.mas_equalTo(self.rightImageView);
    }];
    
    [self.calenderButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.topView.mas_bottom).offset(13 * SCREENH6);
        make.width.height.mas_equalTo(19 * SCREENH6);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.centerY.mas_equalTo(self.calenderButton);
    }];
    
    [self.monthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.titleLabel);
        make.leading.mas_equalTo(self.titleLabel.mas_trailing);
    }];
    
    [self.spaceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.tableView);
         make.centerY.mas_equalTo(self.tableView).offset(-50 * SCREENH6);
        make.width.mas_equalTo(169 * SCREENH6);
        make.height.mas_equalTo(136 * SCREENH6);
    }];
    
    [self.spaceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.spaceImageView);
        make.top.mas_equalTo(self.spaceImageView.mas_bottom).offset(10 * SCREENH6);
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.modelArr.count == 0) {
        
        self.spaceLabel.hidden = NO;
        self.spaceImageView.hidden = NO;
    }else{
        
        self.spaceLabel.hidden = YES;
        self.spaceImageView.hidden = YES;
    }
    return self.modelArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyIntegralTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIntegralTableViewCell" forIndexPath:indexPath];
    
            cell.model = self.modelArr[indexPath.row];
    
    return cell;
}

- (void)clickQuestionButton{
    
    HiddenBar;
    ServerStatementViewController * stateVC = [ServerStatementViewController new];
    
    stateVC.url = @"https://findif.cn/integral.html";
    stateVC.titleStr = @"积分问题";
    
    [self.navigationController pushViewController:stateVC animated:YES];
}


- (void)clickExchangeButton{
    
    HiddenBar;
    
    ExchangeViewController * exchangeVC = [ExchangeViewController new];
    
    [self.navigationController pushViewController:exchangeVC animated:YES];
    
}


- (void)presentDateController{
    
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
    datePickManager.isShadeBackgroud = true;
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.delegate = self;
    datePicker.datePickerType = PGDatePickerType1;
    datePicker.isHiddenMiddleText = false;
    datePicker.datePickerMode = PGDatePickerModeYearAndMonth;
    datePicker.maximumDate = [NSDate date];
    datePicker.language = @"zh-Hans";
    [self presentViewController:datePickManager animated:false completion:nil];
}


#pragma PGDatePickerDelegate
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    
    NSString * date = [NSString stringWithFormat:@"%li-%02li",(long)dateComponents.year,dateComponents.month];
    
    self.monthLabel.text = [NSString stringWithFormat:@"(%@)",date];
//    NSLog(@"date===%@",date);
    
    [[ZRNetworkTool sharedTool] getUserIntegralWithDate_time:date WithBlock:^(BOOL success, id responseData) {
        
        self.modelArr = nil;
        if (success) {
            
            self.modelArr = [NSArray yy_modelArrayWithClass:[MyIntegralModel class] json:responseData[@"data"]];
        }
    }];
}

- (void)clickCalenderButton{
    
    [self presentDateController];
}


#pragma mark - TableView 占位图
- (NSString *)xy_noDataViewMessage {
    
    return @"";
}


@end
